import app.Login;

import com.mt.system.domain.LoginLog;
import com.mt.components.message.Message;
import com.mt.system.domain.User;
import com.mt.components.log.LogFactory;
import com.mt.components.log.Logger;
import com.mt.components.tab.TabFactory;
import com.mt.components.utils.ConstUtils;
import com.mt.components.utils.RemoteFactory;
import com.mt.components.utils.StringUtils;

import flash.events.Event;
import flash.events.HTTPStatusEvent;
import flash.events.MouseEvent;
import flash.net.URLLoader;
import flash.net.URLLoaderDataFormat;
import flash.net.URLRequest;
import flash.utils.ByteArray;

import mx.collections.ArrayCollection;
import mx.controls.Alert;
import mx.core.FlexGlobals;
import mx.events.FlexEvent;
import mx.rpc.events.ResultEvent;
import mx.rpc.remoting.mxml.RemoteObject;

/**      
 * 文件名称：LoginAs.as 
 * 文件描述：
 * 创建人：马腾
 * 创建时间：Nov 22, 2012 10:17:17 PM
 * 修改人：马腾
 * 修改时间：Nov 22, 2012 10:17:17 PM
 */

private var logger:Logger = LogFactory.getLogger(Login);

private var userNameCache:String;

protected function initData(event:FlexEvent):void {
	validateIamge_clickHandler();
}

/**
 *用户登录 
 */
private function login():void {
	if(StringUtils.isEmpty(username.text)){
		message.text = "请输入用户名称！";
		return;
	}
	if(StringUtils.isEmpty(password.text)){
		message.text = "请输入密码！";
		return;
	}
	if(StringUtils.isEmpty(validate.text)){
		message.text = "请输入验证码！";
		return;
	}
	message.text = "";
	username.enabled = false;
	password.enabled = false;
	validate.enabled = false;
	bar.visible = true;
	userNameCache = username.text;
	var remote:RemoteObject = RemoteFactory.getInstance().getRemote("UserService");
	remote.login(username.text , password.text , validate.text);
	remote.addEventListener(ResultEvent.RESULT , resultLogin);
}

protected function resultLogin(event:ResultEvent):void {
	var mes:Message = event.result as Message;
	bar.visible = false;
	message.text = "";
	username.text = "";
	password.text = "";
	validate.text = "";
	username.enabled = true;
	password.enabled = true;
	validate.enabled = true;
	if(mes.type == Message.SUCCESS){
		FlexGlobals.topLevelApplication.user = mes.object as User;
		logger.debug(mes.object.name + " login success");
		loadPermission();
		wirteLog();
	}else{
		validateIamge_clickHandler();
		message.text = mes.result;
		username.text = userNameCache;
	}
}

/**
 * 
 * 记录用户的登录日志
 */
private function wirteLog():void {
	var loginLog:LoginLog = new LoginLog();
	loginLog.user = FlexGlobals.topLevelApplication.user;
	var remote:RemoteObject = RemoteFactory.getInstance().getRemote("LogService");
	remote.addLoginLog(loginLog);
}

/**
 *加载用户的权限 
 */
private function loadPermission():void {
	var remote:RemoteObject = RemoteFactory.getInstance().getRemote("PermissionService");
	remote.addEventListener(ResultEvent.RESULT , resutlPermission);
	remote.getPermissionTreeByUser(FlexGlobals.topLevelApplication.user);
}

protected function resutlPermission(event:ResultEvent):void {
	var permissions:ArrayCollection = event.result as ArrayCollection;
	if(permissions.length == 0){
		message.text = "该用户没有任何权限，请联系管理员赋权！";
		return;
	}
	logger.debug("get user's permissions success");
	FlexGlobals.topLevelApplication.permissions = permissions;
	FlexGlobals.topLevelApplication.vk.selectedIndex = 1;
}

protected function validateIamge_clickHandler():void {
	var imageUrl:String = ConstUtils.basePath + "/ValidateCode";
	var loader:URLLoader = new URLLoader();
	loader.dataFormat = URLLoaderDataFormat.BINARY;
	loader.addEventListener(Event.COMPLETE, onComplete);
	loader.load(new URLRequest(imageUrl));
}

protected function onComplete(event:Event):void {
	var data:ByteArray = event.target.data;
	validateIamge.source = data;
	validateIamge.visible = true;
}
